package com.example.homework

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.homework.utils.RankUtils
import android.util.Log

class RankFragment : Fragment() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var rankAdapter: RankAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_rank, container, false)

        recyclerView = view.findViewById(R.id.recycler_rank)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())

        // 初始化适配器
        rankAdapter = RankAdapter()
        recyclerView.adapter = rankAdapter

        // 从后端获取数据并更新适配器
        loadRankData()

        return view
    }

    private fun loadRankData() {
        // 模拟从后端获取数据
        val users = RankUtils.getUserData(requireContext())
        Log.d("RankFragment", "Loaded users: $users")
        rankAdapter.updateData(users)
    }
}